﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Financiera
{
    
    public partial class Form_Caja : Form
    {
        int codCaja;
        string fecha_vs;
        BindingManagerBase bmCaja;

        public void CargaGrillaCaja()
        {
            if (Conexion.ds_financiera.Tables["Caja"] != null)
                Conexion.ds_financiera.Tables["Caja"].Clear();
            string ConsultaSQL = "Select cod_caja_c as 'Cod Caja',cod_moneda_c as 'Cod Moneda',comprado_c as 'Comprado',vendido_c as 'Vendido',monto_ini_c as 'Monto Inicial',monto_final_c 'Monto Final',fecha as 'Fecha' from Caja where fecha = " + "'" + fecha_vs + "'";
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = Conexion.rutaDB;
            SqlDataAdapter adaptador = new SqlDataAdapter(ConsultaSQL, cn);
            cn.Open();
            Conexion.ds_financiera.Clear();
            adaptador.Fill(Conexion.ds_financiera, "Caja");
            grillaCaja.DataSource = Conexion.ds_financiera.Tables["Caja"];

        }
        string rutaFinancieraSQL = Conexion.rutaDB;
        public Form_Caja()
        {
            InitializeComponent();
        }
        private void CargaComboBox() {
            if (cmbMonedaCaja.Items.Count > 0)
            {
                cmbMonedaCaja.Items.Clear();
            }

            //se declara una variable de tipo SqlConnection
            SqlConnection cn = new SqlConnection();
            //se indica la cadena de conexion
            cn.ConnectionString = Conexion.rutaDB;
            //código para llenar el comboBox
            DataSet ds = new DataSet();
            //indicamos la consulta en SQL
            SqlDataAdapter da = new SqlDataAdapter("SELECT DISTINCT Cod_moneda_a FROM Moneda_actual", cn);
            //se indica el nombre de la tabla
            da.Fill(ds, "Moneda_actual");

            foreach (DataRow row in ds.Tables["Moneda_actual"].Rows)
            {
                cmbMonedaCaja.Items.Add(row[0].ToString());
            }
        }

        private void Form_Caja_Load(object sender, EventArgs e)
        {
            fecha_vs = Conexion.fecha_d + "." + Conexion.fecha_m + "." + Conexion.fecha_a;
            Convert.ToDateTime(fecha_vs);
            CargaGrillaCaja();
            txtFecha.Text = Conexion.fecha_d + "/" + Conexion.fecha_m + "/" + Conexion.fecha_a;
            CargaComboBox();
            bmCaja = BindingContext[Conexion.ds_financiera, "Caja"];
            CargaTextBox();
            txtGrillaCaja.Text = grillaCaja.Rows.Count.ToString();
            txtGrillaCargaCaja.Text = grillaCargaCajas.Rows.Count.ToString();
        }

        private void chkEditarCaja_CheckedChanged(object sender, EventArgs e)
        {
            if (chkEditarCaja.Checked)
            {
                gbEditarCaja.Enabled = true;
                btnGuardar.Enabled = true;
            }
            else
            {
                gbEditarCaja.Enabled = false;
                btnGuardar.Enabled = false;
            }
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            fecha_vs = txtFecha.Text;
            Convert.ToDateTime(fecha_vs);
            string ConsultaSQL;
            ConsultaSQL = "execute Modifica_Caja '" + codCaja + "'," + Convert.ToSingle(txtComprado.Text) + "," + Convert.ToSingle(txtVendido.Text) + "," + Convert.ToSingle(txtInicial.Text) + "," + Convert.ToSingle(txtFinal.Text) + ", '" + fecha_vs + "'";
            SqlConnection cn = new SqlConnection(Conexion.rutaDB);
            cn.Open();
            SqlCommand comando = new SqlCommand(ConsultaSQL, cn);
            comando.ExecuteNonQuery();
            CargaGrillaCaja();
        }

        private void btn_iniciar_caja_Click(object sender, EventArgs e)
        {
            btnIniciarCaja.Enabled = false;
            if (Conexion.ds_financiera.Tables["Caja"] != null)
                Conexion.ds_financiera.Tables["Caja"].Clear();
            grillaCaja.Visible = false;
            string ConsultaSQLOperaciones = "Select cod_caja_c as 'Cod Caja',cod_moneda_c as 'Cod Moneda',comprado_c as 'Comprado',vendido_c as 'Vendido',monto_ini_c as 'Monto Inicial',monto_final_c 'Monto Final',fecha as 'Fecha' from Caja order by fecha desc";
            metodos.AgregarTablaDataSetSQLServer(rutaFinancieraSQL, ConsultaSQLOperaciones,
                                         "Caja", ref Conexion.ds_financiera);
            grillaCargaCajas.DataSource = Conexion.ds_financiera.Tables["Caja"];
            grillaCargaCajas.Visible = true;
            bntCargar.Enabled = true;
            btnCancelar.Enabled = true;
            txtGrillaCaja.Text = grillaCaja.Rows.Count.ToString();
            txtGrillaCargaCaja.Text = grillaCargaCajas.Rows.Count.ToString();
        }

        private void CargaTextBox()
        {
            txtInicial.DataBindings.Add(new Binding("Text", Conexion.ds_financiera,
           "Caja." + Conexion.ds_financiera.Tables["Caja"].Columns[5].ColumnName.ToString()));
            cmbMonedaCaja.DataBindings.Add(new Binding("Text", Conexion.ds_financiera,
           "Caja." + Conexion.ds_financiera.Tables["Caja"].Columns[1].ColumnName.ToString()));
            txtCodCaja.DataBindings.Add(new Binding("Text", Conexion.ds_financiera,
           "Caja." + Conexion.ds_financiera.Tables["Caja"].Columns[0].ColumnName.ToString()));
        }

        private void grillaCargaCajas_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string ConsultaSQL = "execute Agrega_inicial '" + cmbMonedaCaja.Text + "'," + 0.0 + "," +
              0.0 + "," + Convert.ToSingle(txtInicial.Text) + "," + 0.0 + ",'" + fecha_vs + "'";
            SqlConnection cn = new SqlConnection(Conexion.rutaDB);
            cn.Open();
            SqlCommand comando = new SqlCommand(ConsultaSQL, cn);
            comando.ExecuteNonQuery();
            cn.Close();
            btnGuardar.Enabled = false;
            grillaCargaCajas.Visible = false;
            grillaCaja.Visible = true;
            CargaGrillaCaja();
        }

        private void grillaCargaCajas_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (grillaCaja.Visible == false)
            {
                bmCaja.Position = e.RowIndex;
                if(txtCodCaja.Text != "")
                codCaja = Convert.ToInt32(txtCodCaja.Text);
            }
        }

        private void bntCargar_Click(object sender, EventArgs e)
        {
            /*for (int i = 1; i < grillaCaja.Rows.Count; i++) {
                if (grillaCaja.Rows[i].Cells[1].Value.ToString() == cmbMonedaCaja.Text) {
                    MessageBox.Show("Ya se cargo la caja de esa moneda", "Error");
                    i = grillaCaja.Rows.Count;
                }
              else{*/
                    btnIniciarCaja.Enabled = true;
                    string ConsultaSQL = "execute Agrega_inicial '"+ cmbMonedaCaja.Text + "'," + 0.0 + "," +
                      0.0 + "," + Convert.ToSingle(txtInicial.Text) + "," + Convert.ToSingle(txtInicial.Text) + ",'" + fecha_vs + "'";
                    SqlConnection cn = new SqlConnection(Conexion.rutaDB);
                    cn.Open();
                    SqlCommand comando = new SqlCommand(ConsultaSQL, cn);
                    comando.ExecuteNonQuery();
                    cn.Close();
                    btnGuardar.Enabled = false;
                    grillaCargaCajas.Visible = false;
                    grillaCaja.Visible = true;
                    CargaGrillaCaja();
                //}
            //}
        }

        private void bntCancelar_Click(object sender, EventArgs e)
        {
            btnIniciarCaja.Enabled = true;
            btnGuardar.Enabled = false;
            btnCancelar.Enabled = false;
            grillaCargaCajas.Visible = false;
            grillaCaja.Visible = true;
            CargaGrillaCaja();
        }

        private void grillaCaja_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (grillaCargaCajas.Visible == false)
            {
                bmCaja.Position = e.RowIndex;
                if(txtCodCaja.Text != "")
                codCaja = Convert.ToInt32(txtCodCaja.Text);
            }
        }

        private void Form_Caja_FormClosing(object sender, FormClosingEventArgs e)
        {
            metodos.LimpiaDataSet(ref Conexion.ds_financiera);
        }
    }
}
